package com.atomgit.ai.proxy.util;

import com.alibaba.fastjson.JSON;
import com.atomgit.ai.proxy.processor.service.ParamAndResponseProcessor;
import com.atomgit.ai.proxy.vo.ModelAnswerVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RequestCallback;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

@Component
@Slf4j
public class HTTPUtil {



    public String sendRequest(String url, HttpMethod httpMethod, String requestBody,String apiKey) {
        log.info("sendRequest url:{},httpMethod:{},requestBody:{}",url,httpMethod,requestBody);
        try {
            RestTemplate restTemplate = new RestTemplate();
            // 设置请求头
            MultiValueMap<String, String> headersParam = new HttpHeaders();
            HttpHeaders headers = new HttpHeaders(headersParam);
            headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
            headers.setContentType(MediaType.APPLICATION_JSON);
            // 添加其他需要的头部信息
            headers.add("Authorization", apiKey);
            HttpEntity<String> requestEntity = new HttpEntity<>(requestBody,headers);
            // 发送请求
            ResponseEntity<String> response = restTemplate.exchange(url,httpMethod, requestEntity,String.class);
            // 获取响应
            log.info("sendRequest response:{}",response);
            String responseBody = response.getBody();
            return responseBody;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
